/**
 * 
 */
package widgets;

import java.awt.*;
import elements.Node;

/**
 * @author danielsmw
 *
 */
public class NodeWidget extends Point {

	// This is the computable note which I visualize.
	private Node node;
	
	// What space do I occupy?
	private Rectangle area;
	
	private int focus;
	
	private int width;
	
	/**
	 * @param int x - The x-coordinate of the node on the canvas.
	 * @param int y - The y-coordinate of the node on the canvas.
	 */
	public NodeWidget(int x, int y, Node n) {
		super(x,y);
		node = n;
		associateWithNode(n);
		width = 20;
		focus = 0;
		area = new Rectangle((int)this.getX()-width/2, (int)this.getY()-width/2, width, width);
	}
	
	public void associateWithNode(Node n, boolean remoteRequest) {
		node = n;
	}
	
	private void associateWithNode(Node n) {
		node = n;
		n.associateWithWidget(this, true);
	}
	
	public void setUnfocused () {
		focus = 0;
	}
	
	public void setFocused () {
		focus = 1;
	}
	
	public void setSelected () {
		focus = 2;
	}
	
	public int getFocus() {
		return focus;
	}
	
	public void drawMe(Graphics2D g2d) {
		Color oldColor = g2d.getColor();
		g2d.setColor(Color.BLACK);
		int fontHeight = 10;
		g2d.drawRect((int)this.getX()-width/2, (int)this.getY()-width/2, width, width);
		if (focus==1) {
			g2d.setColor(Color.CYAN);
			g2d.fillRect((int)this.getX()-width/2, (int)this.getY()-width/2, width, width);
		}
		if (focus==2) {
			g2d.setColor(Color.BLUE);
			g2d.fillRect((int)this.getX()-width/2, (int)this.getY()-width/2, width, width);
		}
		g2d.drawRect((int)this.getX()-width/2, (int)this.getY()-width/2, width, width);
		g2d.drawString(
				"("+node.getUploadSpeed()+"ul/"+node.getDownloadSpeed()+"dl)",
				(int)this.getX()-width/2,
				(int)this.getY()-width/2-fontHeight);
		g2d.setColor(oldColor);
	}
	
	public Node getNode() {
		return node;
	}
	
	public boolean encompasses(Point p) {
		System.out.println("Checking if "+p+" lies in "+area);
		boolean ret = area.contains(p);
		if (ret) { System.out.println("It does!"); }
		return ret;
	}
	

}
